﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.ComponentModel
    <ToolboxBitmap(GetType(FaultHandlerActivity), "Resources.Exception.png"), Designer(GetType(FaultHandlerActivityDesigner), GetType(IDesigner)), ActivityValidator(GetType(FaultHandlerActivityValidator)), SRCategory("Standard"), SRDescription("FaultHandlerActivityDescription"), ToolboxItem(GetType(ActivityToolboxItem))> _
    Public NotInheritable Class FaultHandlerActivity
        Inherits CompositeActivity
        Implements IActivityEventListener([Of] ActivityExecutionStatusChangedEventArgs), ITypeFilterProvider, IDynamicPropertyTypeProvider
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal name As String)
            MyBase.New(name)
        End Sub

        Protected Friend Overrides Function Cancel(ByVal executionContext As ActivityExecutionContext) As ActivityExecutionStatus
            Return SequenceHelper.Cancel(Me, executionContext)
        End Function

        Protected Friend Overrides Function Execute(ByVal executionContext As ActivityExecutionContext) As ActivityExecutionStatus
            Return SequenceHelper.Execute(Me, executionContext)
        End Function

        Protected Friend Overrides Sub Initialize(ByVal provider As IServiceProvider)
            If (MyBase.Parent Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("Error_MustHaveParent"))
            End If
            MyBase.Initialize(provider)
        End Sub

        Protected Friend Overrides Sub OnActivityChangeRemove(ByVal executionContext As ActivityExecutionContext, ByVal removedActivity As Activity)
            SequenceHelper.OnActivityChangeRemove(Me, executionContext, removedActivity)
        End Sub

        Protected Friend Overrides Sub OnWorkflowChangesCompleted(ByVal executionContext As ActivityExecutionContext)
            SequenceHelper.OnWorkflowChangesCompleted(Me, executionContext)
        End Sub

        Friend Sub SetException(ByVal e As Exception)
            MyBase.SetValue(FaultHandlerActivity.FaultProperty, e)
        End Sub

        Private Function System.Workflow.ComponentModel.Design.ITypeFilterProvider.CanFilterType(ByVal type As Type, ByVal throwOnError As Boolean) As Boolean Implements ITypeFilterProvider.CanFilterType
            Dim flag As Boolean = TypeProvider.IsAssignable(GetType(Exception), type)
            If (throwOnError AndAlso Not flag) Then
                Throw New Exception(SR.GetString("Error_ExceptionTypeNotException", New Object() { type, "Type" }))
            End If
            Return flag
        End Function

        Private Sub System.Workflow.ComponentModel.IActivityEventListener<System.Workflow.ComponentModel.ActivityExecutionStatusChangedEventArgs>.OnEvent(ByVal sender As Object, ByVal e As ActivityExecutionStatusChangedEventArgs) Implements IActivityEventListener([Of] ActivityExecutionStatusChangedEventArgs).OnEvent
            SequenceHelper.OnEvent(Me, sender, e)
        End Sub

        Private Function System.Workflow.ComponentModel.IDynamicPropertyTypeProvider.GetAccessType(ByVal serviceProvider As IServiceProvider, ByVal propertyName As String) As AccessTypes Implements IDynamicPropertyTypeProvider.GetAccessType
            If (propertyName Is Nothing) Then
                Throw New ArgumentNullException("propertyName")
            End If
            If propertyName.Equals("Fault", StringComparison.Ordinal) Then
                Return AccessTypes.Write
            End If
            Return AccessTypes.Read
        End Function

        Private Function System.Workflow.ComponentModel.IDynamicPropertyTypeProvider.GetPropertyType(ByVal serviceProvider As IServiceProvider, ByVal propertyName As String) As Type Implements IDynamicPropertyTypeProvider.GetPropertyType
            If (propertyName Is Nothing) Then
                Throw New ArgumentNullException("propertyName")
            End If
            Dim faultType As Type = Nothing
            If String.Equals(propertyName, "Fault", StringComparison.Ordinal) Then
                faultType = Me.FaultType
                If (faultType Is Nothing) Then
                    faultType = GetType(Exception)
                End If
            End If
            Return faultType
        End Function


        ' Properties
        <SRDescription("FaultDescription"), MergableProperty(False), [ReadOnly](True)> _
        Public ReadOnly Property Fault As Exception
            Get
                Return [TryCast](MyBase.GetValue(FaultHandlerActivity.FaultProperty),Exception)
            End Get
        End Property

        <Editor(GetType(TypeBrowserEditor), GetType(UITypeEditor)), MergableProperty(False), SRDescription("ExceptionTypeDescr")> _
        Public Property FaultType As Type
            Get
                Return DirectCast(MyBase.GetValue(FaultHandlerActivity.FaultTypeProperty), Type)
            End Get
            Set(ByVal value As Type)
                MyBase.SetValue(FaultHandlerActivity.FaultTypeProperty, value)
            End Set
        End Property

        Private ReadOnly Property System.Workflow.ComponentModel.Design.ITypeFilterProvider.FilterDescription As String
            Get
                Return SR.GetString("FilterDescription_FaultHandlerActivity")
            End Get
        End Property


        ' Fields
        Friend Shared ReadOnly FaultProperty As DependencyProperty = DependencyProperty.Register("Fault", GetType(Exception), GetType(FaultHandlerActivity))
        Public Shared ReadOnly FaultTypeProperty As DependencyProperty = DependencyProperty.Register("FaultType", GetType(Type), GetType(FaultHandlerActivity), New PropertyMetadata(DependencyPropertyOptions.Metadata))
    End Class
End Namespace

